package com.sgcc.sx.pmos.mobileapp.presenter;


import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.sx.pmos.common.base.BasePresenter;
import com.sgcc.sx.pmos.common.bean.ImageInfoBean;
import com.sgcc.sx.pmos.common.bean.UploadData;
import com.sgcc.sx.pmos.common.bean.UploadDataBean;
import com.sgcc.sx.pmos.common.http.HttpInterConst;
import com.sgcc.sx.pmos.common.http.HttpRequestUtils;
import com.sgcc.sx.pmos.common.http.MvpNetCallback;
import com.sgcc.sx.pmos.common.http.UploadUtil;
import com.sgcc.sx.pmos.mobileapp.bean.QueryBusinessInfoBean;
import com.sgcc.sx.pmos.mobileapp.bean.StatusBean;
import com.sgcc.sx.pmos.mobileapp.contract.BusinessInfoContract;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class BusinessInfoPresenter extends BasePresenter<BusinessInfoContract.View>
        implements BusinessInfoContract.Presenter {


    @Override
    public void uploadFile(String filePath) {
        File file = new File(filePath);
        UploadUtil.upload("", "101",
                "工商营业执照", "", file.getName(), file,
                new MvpNetCallback<UploadDataBean>(getView(), true) {
                    @Override
                    public void onResponse(UploadDataBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response && null != response.getData() && response.getData().size() > 0) {
                            UploadData uploadData = response.getData().get(0);
                            getImageInfo(uploadData.getFileId(), "1");
                        }


                    }


                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        getView().onFailed(0);
                    }
                });


    }

    @Override
    public void queryBusinessInfo() {

        HttpRequestUtils.getInstance().request(new HashMap<>(), HttpInterConst.QUERY_BUSINESS_INFO,
                new MvpNetCallback<QueryBusinessInfoBean>(getView(), true) {
                    @Override
                    public void onResponse(QueryBusinessInfoBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response) {
                            if (response.getStatus() == 0)
                                getView().queryBusinessInfoSuccess(response.getData());

                        }


                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        getView().onFailed(2);

                    }
                });
    }

    @Override
    public void saveData(Map<String, Object> params) {


        HttpRequestUtils.getInstance().request(params, HttpInterConst.SAVE_BUSINESS_INFO,
                new MvpNetCallback<StatusBean>(getView(), true) {
                    @Override
                    public void onResponse(StatusBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response) {
                            if (response.getStatus() == 0)
                                getView().saveDataSuccess(response.getMessage());
                            else
                                ToastUtils.showShort(response.getMessage());

                        }


                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        getView().onFailed(2);

                    }
                });


    }


    private void getImageInfo(String fileId, String ocrType) {


        Map<String, Object> params = new HashMap<>();
        params.put("fileId", fileId); //文件id
        params.put("ocrType", ocrType);//1工商信息，2法定代表人，3银行信息

        HttpRequestUtils.getInstance().request(params, HttpInterConst.IMAGERECOGNITION,
                new MvpNetCallback<ImageInfoBean>(getView(), true) {
                    @Override
                    public void onResponse(ImageInfoBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response && null != response.getData()) {
                            ImageInfoBean.Info data = response.getData();
                            getView().uploadFileSuccess(data);
                        } else {
                            getView().onFailed(1);
                            ToastUtils.showShort("识别失败,请重新选择照片或者手动填写！");
                        }


                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        getView().onFailed(1);

                    }
                });


    }


}
